package com.yh.workbill.ui.count;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.yh.workbill.R;
import com.yh.workbill.adapter.CountAdapter;
import com.yh.workbill.base.BaseFragment;
import com.yh.workbill.databinding.FragmentCountBinding;
import com.yh.workbill.model.Count;
import com.yh.workbill.ui.count.mvp.Count_Presenter;
import com.yh.workbill.ui.count.mvp.Count_View;
import com.yh.workbill.ui.countinfo.CountInfoActivity;
import com.yh.workbill.utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class CountFragment extends BaseFragment implements Count_View {

    private CountViewModel viewModel;
    private FragmentCountBinding binding;
    private CountAdapter adapter;
    private Count_Presenter presenter;
    private int pageindex = 1, pagenumber = 20;
    private List<Count> listCount = new ArrayList<>();

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_count, container, false);
        viewModel = ViewModelProviders.of(this).get(CountViewModel.class);
        return binding.getRoot();
    }

    @Override
    public void initJetPck() {
        binding.setLifecycleOwner(requireActivity());
    }

    @Override
    public void initSetting() {
        presenter = new Count_Presenter(this);
    }

    @Override
    public void initObserver() {
        requireActivity().getLifecycle().addObserver(presenter);
        viewModel.getCountDatas().observe(getViewLifecycleOwner(), new Observer<List<Count>>() {
            @Override
            public void onChanged(List<Count> counts) {

//                for (Count bean : counts) {
//                    Log.e("TAG", "统计的数据是:" + bean.getCompany_name() + " " + bean.getCount() + " " + bean.getMoney() + "\n");
//                }

                adapter.setData(counts);
            }
        });
    }

    @Override
    public void initDefaultMethod() {
        presenter.getCounts(viewModel, false, true, false, pageindex, pagenumber);
    }

    @Override
    public void onResume() {
        super.onResume();
//        pageindex = 1;
//        if (listCount.size() > 0) {
//            presenter.getCounts(viewModel, false, true, false, pageindex, pagenumber);
//        } else {
//            presenter.getCounts(viewModel, true, true, false, pageindex, pagenumber);
//        }
    }

    @Override
    public void initData() {
        adapter = new CountAdapter(listCount);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        binding.recyclerView.setAdapter(adapter);

    }

    @Override
    public void initClick() {
        binding.refreshLayout.setOnRefreshListener(refreshLayout -> {
            pageindex = 1;
            presenter.getCounts(viewModel, true, true, false, pageindex, pagenumber);
        });

        binding.refreshLayout.setOnLoadMoreListener(refreshLayout ->
                presenter.getCounts(viewModel, true, false, true, pageindex, pagenumber));
        adapter.setOnItemClick(new CountAdapter.OnItemClick() {
            @Override
            public void onItem(Count count, int position) {
                Intent i = new Intent(requireActivity(), CountInfoActivity.class);
                i.putExtra("title", count.getCompany_name());
                startActivity(i);
            }
        });
    }

    @Override
    public void getCounts(List<Count> counts) {
        pageindex++;
    }

    @Override
    public void gotoItemInfo(String companyName) {

    }

    @Override
    public void refresh() {
        binding.refreshLayout.finishRefresh();
    }

    @Override
    public void loadMore() {
        binding.refreshLayout.finishLoadMore();
    }

    @Override
    public void fail(String msg) {
        Utils.ShowToas(requireActivity(), msg);
        binding.refreshLayout.finishRefresh();
        binding.refreshLayout.finishLoadMore();
    }

    @Override
    public void showTasot(String message) {
        Utils.ShowToas(requireActivity(), message);
    }

    @Override
    public void showDialog(String content, boolean isCancelable) {
        showLoaddingDialog(content, isCancelable);
    }

    @Override
    public void hideDialog() {
        hideLoaddingDialog();
    }
}